package com.example.everying.service.statistics.impl;/*
 * 项目名称: everything
 * 文件名: UserBehaviorLogServiceImpl
 * 作者: 张泽阳
 * 日期: 2025/7/17
 * 描述:
 */

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.everying.mapper.statistics.UserBehaviorLogMapper;
import com.example.everying.model.entity.statistics.UserBehaviorLog;
import com.example.everying.service.statistics.UserBehaviorLogService;
import com.example.everying.service.statistics.UserSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class UserBehaviorLogServiceImpl extends ServiceImpl<UserBehaviorLogMapper, UserBehaviorLog> implements UserBehaviorLogService {
    @Autowired
    private UserSessionService userSessionService;

    @Override
    @Async
    @Transactional(rollbackFor = Exception.class)
    public boolean addUserBehaviorLog(UserBehaviorLog userBehaviorLog) {
        try {
            if (userBehaviorLog.getUserId().isEmpty() || userBehaviorLog.getArticleId().isEmpty()){
                return false;
            }
        }catch (NullPointerException e){
            return false;
        }

        try {
            Long sessionId = userSessionService.getUserSessionByUserId(userBehaviorLog.getUserId()).getId();
            userBehaviorLog.setSessionId(sessionId);
        }catch (NullPointerException e){
            return true;
        }


        userBehaviorLog.setCreateTime(LocalDateTime.now());
        userBehaviorLog.setUpdateTime(LocalDateTime.now());
        return save(userBehaviorLog);
    }
}
